﻿using PBO.Data;
using PBO.Data.PokemonOnline;
using PBO.Data.Util;
using PBO.Properties;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PBO.Util
{
    internal static class FileHelper
    {
        public static TeamData LoadTeam(string path)
        {
            TeamData team = null;
            try
            {
                if (path.EndsWith(".tp"))
                {
                    team = POHelper.ReadFile(path);
                    team.Validate();
                }
                else if (path.EndsWith(".ptd"))
                {
                    team = TeamData.From0791(path);
                    team.Validate();
                }
                Settings.Default.teamPath = path;
                //if (Settings.Default.history.IndexOf(path) == -1)
                //{
                //    Settings.Default.history.Add(path);
                //}
            }
            catch
            {
                Settings.Default.teamPath = string.Empty;
                MessageBox.Show("队伍打开错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return team;
        }

        public static void SaveTeam(string path, TeamData team)
        {
            POHelper.WriteFile(path, team);
        }

        public static string GetLogSavePath(IDictionary<byte, string> players)
        {
            return Path.Combine(Properties.Settings.Default.LogPath, GetFileName(players.Values.ToArray()));
            //string path = string.Empty;
            //if (Properties.Settings.Default.AutoSaveLog || (MessageBox.Show("是否保存战报?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes))
            //{
            //    string name = FileHelper.GetFileName(players.Values.ToArray());
            //    if (!string.IsNullOrEmpty(Properties.Settings.Default.LogPath))
            //    {
            //        path = Path.Combine(Properties.Settings.Default.LogPath, name);
            //    }
            //    else
            //    {
            //        const string Filter = "文本文档(*.txt)|*.txt";
            //        path = FileHelper.SaveFilePath(Filter, name);
            //    }
            //}
            //return path;
        }

        public static string GetFileName(params string[] players)
        {
            int take = players.Length / 2;
            string p1 = string.Join("&", players.Take(take));
            string p2 = string.Join("&", players.Skip(take).Take(take));
            string fileName = string.Format("{0}-{1} vs {2}.txt", DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss_fff"), p1, p2);
            return Regex.Replace(fileName, "[\\\\/:*?\"<>|]", "_", RegexOptions.Compiled);
        }

        public static string OpenFilePath(string filter = "All Types (*.*)|*.*", string dir = "")
        {
            string path = string.Empty;

            using (var od = new OpenFileDialog())
            {
                od.InitialDirectory = dir;
                od.Filter = filter;
                od.Multiselect = false;
                if (od.ShowDialog() == DialogResult.OK)
                {
                    path = od.FileName;
                }
            }
            return path;
        }

        public static string SaveFilePath(string filter = "All Types (*.*)|*.*", string fileName = "")
        {
            string path = string.Empty;
            using (var sd = new SaveFileDialog())
            {
                sd.InitialDirectory = Application.StartupPath;
                sd.Filter = filter;
                sd.FileName = fileName;
                if (sd.ShowDialog() == DialogResult.OK)
                {
                    path = sd.FileName;
                }
            }
            return path;
        }

        public static void WriteToFile(string path, IEnumerable<string> rows)
        {
            using (var sw = new StreamWriter(path, false, Encoding.Unicode))
            {
                try
                {
                    sw.WriteLine("Pokemon Battle Online " + Application.ProductVersion);
                    foreach (var row in rows)
                    {
                        sw.WriteLine(row);
                    }
                }
                finally
                {
                    sw.Flush();
                    sw.Close();
                }
            }
        }

        public static void WriteToFile(string path, DataTable table)
        {
            using (var sw = new StreamWriter(path, false, Encoding.Default))
            {
                try
                {
                    CsvWriter.WriteDataTable(sw, table);
                }
                finally
                {
                    sw.Flush();
                    sw.Close();
                }
            }
        }
    }
}
